package com.weimob.mcloud.wechatwork.customer.mq.handler.losemsg;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bos.response.ProductInstanceDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.ec.request.BatchQueryCouponsAdapterRequest;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.ec.response.CouponListAdapterResponse;
import com.weimob.mcloud.wechatwork.core.adapter.service.CRMAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.customer.domain.CustomerTouchInfo;
import com.weimob.mcloud.wechatwork.customer.facade.impl.sidebar.SidebarFacade;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 优惠券信息
 * Create Time: 2022年05月23日 10:15
 *
 * @author maosheng.li
 **/
@Service
@Slf4j
public class CouponTouchImpl implements CustomerTouchService {

    @Reference
    private CRMAdapterService crmAdapterService;

    @Autowired
    private SidebarFacade sidebarFacade;

    @Value("${mall.productid}")
    private Long mallProductId;

    @Override
    public CustomerTouchInfo getCustomerTouchInfo(PrivateSphere privateSphere, CustomerTouchInfo touchInfoRequest) {
        CustomerTouchInfo result = new CustomerTouchInfo();

        ProductInstanceDTO productByVid = sidebarFacade.getProductByVid(touchInfoRequest.getEcVid(), mallProductId);
        if (Objects.isNull(productByVid)) {
            return null;
        }
        BatchQueryCouponsAdapterRequest batchQueryCouponsAdapterRequest = new BatchQueryCouponsAdapterRequest();
        batchQueryCouponsAdapterRequest.setBosId(privateSphere.getBosId());
        batchQueryCouponsAdapterRequest.setProductId(productByVid.getProductId());
        batchQueryCouponsAdapterRequest.setProductInstanceId(productByVid.getProductInstanceId());
        batchQueryCouponsAdapterRequest.setCouponTemplateIds(Lists.newArrayList(Long.valueOf(touchInfoRequest.getTaskId())));

        SoaResponse<List<CouponListAdapterResponse>, ?> listSoaResponse = crmAdapterService.batchQueryValidMerchantCouponList(batchQueryCouponsAdapterRequest);
        List<CouponListAdapterResponse> responseVo = listSoaResponse.getResponseVo();
        if(CollectionUtils.isNotEmpty(responseVo)){
            CouponListAdapterResponse couponListAdapterResponse = responseVo.get(0);
            result.setTaskName(couponListAdapterResponse.getName());
        }
        return result;
    }
}
